export const nodeTreeList = (treeList: any) => {
  const nodeTree = treeList.filter((item: any) => {
    delete item.children
    item.children = treeList.filter((c: any) => {
      return c.pid === item.id
    })
    return !item.pid
  })
  return nodeTree
}

/**
 * 财务报表按照报名名称/报表类型排序
 */
export function sortCpasTable(newFsData: any, field?: string) {
  let info = field ? field : "name";
  if(!newFsData) {
    return;
  }
  const NAME_SORT_CFG = ["未审财务报表", "试算平衡", "审定财务"]; //一级报名名称排序枚举
  const TYPE_SORT_ORDER = {
    资产: 1,
    负债: 2,
    利润: 3,
    现流: 4,
    现流附: 5,
    "权益变动(本年)": 6,
    "权益变动(上年)": 7,
    减值准备: 8,
  }; //二级报名分类排序枚举
  let nameResData = []; //按报表名称排序结果存储
  for (let i = 0; i < NAME_SORT_CFG.length; i++) {
    let temp: any = [];
    const name = NAME_SORT_CFG[i];
    for (let j = 0; j < newFsData.length; j++) {
      const item: any = newFsData[j];
      const tableName = item[info];
      if (tableName.includes(name)) {
        temp.push(item);
      }
    }
    nameResData.push(temp);
  }
  let typeResData = []; //按报表分类排序结果存储
  for (let i = 0; i < nameResData.length; i++) {
    let groupData = nameResData[i].slice();
    for (let key in TYPE_SORT_ORDER) {
      for (let j = 0; j < groupData.length; j++) {
        if (groupData[j][info].includes(key)) {
          groupData[j]["index"] = TYPE_SORT_ORDER[key];
        }
      }
    }
    groupData.map((item: any) => {
      if (!item.index) {
        item.index = 99;
      }
    });
    const temp = groupData.sort((a: any, b: any) => a.index - b.index);
    typeResData.push(...temp);
  }
  console.log("resData", typeResData);
  return typeResData;
}
/**
 * 根据数组中的number重新排序校验公式
 */
export function sortValidtorData(array: any) {
  if (!array || !array.length) {
    return;
  }
  console.log(array, "array before");
  array.sort(function (a: any, b: any) {
    const aNumber = a.number.toString();
    const bNumber = b.number.toString();
    var aParts = aNumber.split("-");
    var bParts = bNumber.split("-");

    if (aParts[0] === bParts[0]) {
      if (aParts[1] && bParts[1]) {
        return parseInt(aParts[1]) - parseInt(bParts[1]);
      } else {
        return aNumber.localeCompare(bNumber);
      }
    } else {
      return parseInt(aParts[0]) - parseInt(bParts[0]);
    }
  });
  console.log(array, "array after");
  return array;
}

/**
 * 字符串省略多余字符
 * @param value
 * @param len
 */
export function ellipsis(value: string, len: number) {
  if(!value) {
    return;
  }
  if(value.length > len) {
    return value.slice(0, len)+"...";
  }
  return value;
}
